<?php

class Sale_management extends Controller {

    function Sale_management() {
        parent::Controller();
        $this->load->model('mod_sale_management');
    }

    //======================function is use for loading master page=======================//
    function index() {
        if($this->check_session()){
        $this->load->view('master');
        }else{
            redirect('admin/');
            exit();
        }
    }

    //===================this is function for view manager page=====================//

    function manager() {
        if($this->check_session()){
        $data['title'] = ' saleproduct manager';
        // pagination
        $config['base_url'] = base_url() . 'sale_management/manager';
        $config['prev_link'] = 'Previous';
        $config['next_link'] = 'Next';
        $config['per_page'] = ($this->input->post($this->dbf->pro_name)) ? '' : '';
        $config['total_rows'] = ($this->input->post($this->dbf->pro_name)) ? '' : $this->db->where('sal_status', 1)->count_all_results('tbl_sale'); // (!$this->input->post($this->dbf->tea_name) || $this->input->post($this->dbf->tea_name)=='')?$this->db->count_all('tbl_teachers'):$this->mod_teachers->get_teachers(array($config['per_page'], $this->uri->segment(3)))->num_rows();
        $this->load->library('pagination');
        $this->pagination->initialize($config);

        $data['query'] = $this->mod_sale_management->get_saleproduct(array($config['per_page'], $this->uri->segment(3)));
        $this->load->view("master", $data);
        }else{
            redirect('admin');
            exit();
        }
    }

    //========================this is manager card=========================//
    //====================== this is for add card function===============//

    function add_card() {
        if($this->check_session()){
        $this->form_validation->set_rules('student', 'Studnet name', 'required');
        $this->form_validation->set_rules('card_type', 'card type name', 'required');
        $this->form_validation->set_rules('card', 'card', 'required');
        $this->form_validation->set_rules('txt_quantity', 'txt_quantity field', 'required');
        $this->form_validation->set_rules('txt_payment', 'payment', 'required');
        $this->form_validation->set_rules('txt_saleDate', 'txt_saleDate field', 'required');

        if ($this->input->post('btn_submit')) {
            if ($this->form_validation->run() == false) {
                $this->load->view('master');
            } else {
                $this->mod_sale_management->add_card();
                $this->session->set_userdata('add_card_ms', '<br/><dl id="system-message"><dd class="message message"><ul><li>Added Successful!</li></ul></dd></dl>');
                redirect('sale_management/sale_card');
            }
        }
        else
            $this->load->view('sale_management/add_card');
        }else{
            redirect('admin/');
            
            exit();
        }
    }

    //=============================this is function for add new material=================//

    function add_material() {
        if($this->check_session()){
        $this->form_validation->set_rules('customer_name', 'Custormer name', 'required');
        $this->form_validation->set_rules('txt_proname', 'product name', 'required');
        $this->form_validation->set_rules('txt_quantity', 'quantity', 'required');
        $this->form_validation->set_rules('txt_saleDate', 'sale date field', 'required');


        if ($this->input->post('btn_submit')) {
            if ($this->form_validation->run() == false) {
                $this->load->view('master');
            } else {
                $this->mod_sale_management->add_material();
                $this->session->set_userdata('material_ms', '<div valign="middle" style="width:99%; margin:0 atuo; height:auto;border:1px solid #00F;background-color: #C3D2E5; padding:5px; color:#ooF;">' . img('images/user_images/myimage.png', 'align="left" "') . 'sale successful</div><br/><br/>');
                redirect('sale_management/sale_material');
            }
        } else {
            $this->load->view('sale_management/add_material');
        }
        }  else {
            redirect('admin/');
            exit();
        }
    }

    //====================this function for using  editing sell material======================//

    function edit_material() {// if have submit to check edit
        if($this->check_session()){
        if ($this->input->post('btn_submit')) {
            $this->mod_sale_management->check_edit();
            $this->session->set_userdata('edit_sale', '<div valign="middle" style="width:99%; margin:0 atuo; height:auto;border:1px solid #00F;background-color: #C3D2E5; padding:5px; color:#ooF;">' . img('images/user_images/myimage.png', 'align="left" "') . 'Update successful</div><br/><br/>');
            redirect('sale_management/sale_material');
        } else {

            //====================if not submit form date select data come to view in text field=====================//

            $select_value['value_material'] = $this->mod_sale_management->select_edit();
            $this->load->view('master', $select_value);
        }
        }else{
            redirect('admin/');
            exit();
        }
    }

    //======================this is function for detail========================//

    function view_material() {
        if($this->check_session()){
        $detail_material['value_material'] = $this->mod_sale_management->view_detail_material();
        $this->load->view('master', $detail_material);
        }else{
            redirect('admin/');
            exit();
        }
    }

    //===========this is function for view card=============================//

    function view_card() {
        if($this->check_session()){
        $detail_cardl['value_card'] = $this->mod_sale_management->view_detail_card();
        $this->load->view('master', $detail_cardl);
        }else{
            redirect('admin/');
            exit();
        }
    }

//========================this function for delete material=========================//

    function delete_material() {
        if($this->check_session()){
        $this->mod_sale_management->delete_material();
         $this->session->set_userdata('del_salemanagment', '<div valign="middle" style="width:99%; margin:0 atuo; height:auto;border:1px solid #00F;background-color: #C3D2E5; padding:5px; color:#ooF;">' . img('images/user_images/myimage.png', 'align="left" "') . 'Delete sale successful</div><br/><br/>');
        redirect('sale_management/sale_material');
        }else{
            redirect('admin/');
            exit();
        }
    }

//======================this is function for delete sale management=======================//

    /* function delete() {
      $this->mod_sale_management->delete();
      $this->session->set_userdata('del_salemanagment', '<div valign="middle" style="width:99%; margin:0 atuo; height:auto;border:1px solid #00F;background-color: #C3D2E5; padding:5px; color:#ooF;">' . img('images/user_images/myimage.png', 'align="left" "') . 'Delete sale successful</div><br/><br/>');
      redirect('sale_management/manager');
      } */

//============================this is function for delete card sale===============================//

    function delet_card() {
        if($this->check_session()){
        $this->mod_sale_management->delete_card();
        $this->session->set_userdata('del_salemanagment', '<div valign="middle" style="width:99%; margin:0 atuo; height:auto;border:1px solid #00F;background-color: #C3D2E5; padding:5px; color:#ooF;">' . img('images/user_images/myimage.png', 'align="left" "') . 'Delete sale successful</div><br/><br/>');
        redirect('sale_management/sale_card');
        }else{
            redirect('admin');
            exit();
        }
        
    }

    //==========================This function is used to edit card sale========================//

    function edit_salecard() {
        if($this->check_session()){
        if ($this->input->post('btn_update')) {
            $this->mod_sale_management->card_edit();
           $this->session->set_userdata('ms_card', '<div valign="middle" style="width:99%;margin:0 auto;height:auto;border:1px solid #00F;background-color: #C3D2E5;padding:5px;color:#ooF;">' . img('images/user_images/myimage.png', ' align="left" "') . 'Add sucessful</div><br/><br/>');
            redirect('sale_management/sale_card');
        } else {
            $data['cardvalue'] = $this->mod_sale_management->editcardsale();
            $this->load->view('master', $data);
        }
        }else{
            redirect('admin');
            exit();
        }
    }

    //======================this is function in order to get card=======================//

    function getCardPrice() {
        if($this->check_session()){
        $price = '';
        $stu_id = $this->input->post('stuid');
        $card_id = $this->input->post('cardid');

        $sql = "select st.*, pc.* from"
                . "\n tbl_stu_type st, tbl_pricecard pc, tbl_card c, tbl_student s"
                . "\n where c.car_id=pc.car_id"
                . "\n AND s.stu_type_id=st.stu_type_id"
                . "\n AND c.car_id=" . $card_id
                . "\n AND s.stu_id=" . $stu_id;

        $re = $this->db->query($sql);

        foreach ($re->result() as $row) {
            $field = "price_" . $row->stu_type_name;
            $price = $row->$field;
        }
        echo $price;
        }else{
            redirect('admin/');
            exit();
        }
    }

    //=======================get all card name according to its type=================//

    function getCardByType() {
        if($this->check_session()){
        $opt = '';
        $card_type_id = $this->input->post('type_id');

        $sql = "select c.* from tbl_card c, tbl_car_type ct"
                . "\n where ct.car_type_id = c.car_type_id"
                . "\n AND c.car_type_id = " . $card_type_id;

        $re = $this->db->query($sql);

        $opt .= "<option value='0'>-- Select One --</option>";
        foreach ($re->result() as $row) {

            $opt .= "<option value='" . $row->car_id . "'>" . $row->car_name . "</option>";
        }
        echo $opt;
        }else{
            redirect('admin/');
            exit();
        }
    }

    //=============View slae material in page sale_management =============//

    function sale_material() {
        if($this->check_session()){
        $data['title'] = ' saleproduct manager';
        // pagination
        $config['base_url'] = base_url() . 'sale_management/sale_material';
        $config['prev_link'] = 'Previous';
        $config['next_link'] = 'Next';
        $config['per_page'] = ($this->input->post($this->dbf->pro_name)) ? '' : '10';
        $config['total_rows'] = ($this->input->post($this->dbf->pro_name)) ? '' : $this->db->where('sal_status', 1)->count_all_results('tbl_sale'); // (!$this->input->post($this->dbf->tea_name) || $this->input->post($this->dbf->tea_name)=='')?$this->db->count_all('tbl_teachers'):$this->mod_teachers->get_teachers(array($config['per_page'], $this->uri->segment(3)))->num_rows();
        $this->load->library('pagination');
        $this->pagination->initialize($config);

        $data['query'] = $this->mod_sale_management->get_saleproduct(array($config['per_page'], $this->uri->segment(3)));
        $this->load->view("master", $data);
        }else{
            redirect('admin/');
            exit();
        }
        
    }

    //=========this is function for view ing sale card=============//

    function sale_card() {
        //  $this->load->view('master');
        if($this->check_session()){
        $data['title'] = ' salecard manager';
        // pagination
        $config['base_url'] = base_url() . 'sale_management/sale_card';
        $config['prev_link'] = 'Previous';
        $config['next_link'] = 'Next';
        $config['per_page'] = ($this->input->post($this->dbf->car_name)) ? '' : '';
        $config['total_rows'] = ($this->input->post($this->dbf->car_name)) ? '' : $this->db->where('sal_status', 1)->count_all_results('tbl_sale');
        $this->load->library('pagination');
        $this->pagination->initialize($config);
        $data['values'] = $this->mod_sale_management->get_salecard(array($config['per_page'], $this->uri->segment(3)));
        $this->load->view("master", $data);
        }else{
            redirect('admin/');
            exit();
        }
    }
    
     //========this is function for using checking session=============
    
     public function check_session() {
        if ($this->session->userdata('use_username')) {
            return true;
        } else {
            return false;
        }
    }

}